/**----------------------------------------------------------*/
/** JS:  cmi_new_list.js（订单）           */
/**----------------------------------------------------------*/

var tipc = 'ORDER';
var tipc_search_goods_variety = 'GOODS_VARIETY';
var win = null;

var add = function() {
	try {
		var div = getEditDiv(null);
		
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitAdd, panel + "--添加","保存提交");
		win.window({
			width:800,
			height:500,
			modal: true
		});
	} catch (e) {
		showJsException(e);
	}
};

var modify = function() {
	try{
		var record = datagridOneRow('dataPanel');
		
		if(record == null) {
			return;
		}
		var div = getEditDiv(record);
		
		win = MyUtils._getToolbarWin("dataPanel", "editform", div, submitModify, panel +"--修改","保存提交");
		win.window({
			width:900,
			height:500,
			modal: true
		});
		
		initContent();
		loadNewsTypeList();
		initDate();
		
		repainNews(record);
	} catch(e) {
		showJsException(e);
	}
};

var remove = function() {
	try{
		var rows = $('#dataPanel').datagrid("getSelections");
		if(rows.length == 0) {
			MyUtils.waring(_empty_select);
			return;
		}
		var ids = new Array();
		var names = new Array();
		for(var i = 0; i < rows.length; i++) {
			ids.push(rows[i].uuid);
			names.push(rows[i].brandName);
		}
		var data = { data:"["+MyUtils.createQueryConditionBean("uuid","customer","uuid IN (" + ids.toString() + ")","0",null) +"]"
			    ,st:tipc
			    ,msg:names.toString()
		};
		$.messager.confirm(_msg_sys,MyUtils.valueAlign(_remove_confirm,"left"), function(r) {
			if (r) {
				MyUtils.openMask(_msg_process,$('#t2'));
				MyUtils.sendPost("impl_remove/backRemove.adm",data,function(rets){
					try{
						MyUtils.hiddenMask($('#t2'));
						var res = MyUtils.toJsonObject(rets);
						if(res.error == '0') {
							MyUtils.info(res.msg);
							$('#dataPanel').datagrid("clearSelections");
							$('#dataPanel').datagrid("reload");
						} else {
							MyUtils.error(res.msg);
						}
					} catch (e) {
						showJsException(e);
					}
				});
			}
		});
		
	} catch(e) {
		showJsException(e);
	}
};

var dbclick = function(index,row){
	$('#dataPanel').datagrid("clearSelections");
	$('#dataPanel').datagrid("selectRow",index);
	modify();
};
var loadTypeList = function() {
	MyUtils.sendPost("impl_search/searchObjectList.ajax",{qy:"[]",st:tipc_search_goods_variety},function(msg){
		try{
			var rest = MyUtils.toJsonObject(msg);
			if(rest.length > 0) {
				var opt='';
				for(var i = 0; i < rest.length; i++) {
					opt += '<option value="'+rest[i].uuid+'">'+rest[i].varName+'</option>';
				}
				$('#varietySearch').append(opt);
			}
		} catch (e) {
		}
	});
};
var initPanel = function() {
	var columns = [
	               
	               {field : 'uuid',title : '(ID)编号',width : 40,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
				    , {field : 'orId',title : '订单号',width : 150,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
				    ,{field : 'orName',title : '订单人',width : 50,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
					,{field : 'orPhone',title : '联系电话',width : 150,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
					,{field : 'createTime',title : '订购时间',width : 150,sortable:true,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"center");}}
					];
	var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.asc};
	MyUtils.createPaginPanelWithQueryparams('dataPanel',null,Dmlx._hFrozenColumns,columns,toolbars,dbclick, panel,null);
	$('#dataPanel').datagrid({
		url:"impl_search/commonsPaginSearch.ajax",
		queryParams:pam,
		pagination:true,
		fitColumns:true
	});
	$('#search').linkbutton({
		plain : true
	}).click(function(){
		var selected = $('#varietySearch').val();
		var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.desc};
		if(selected !=  '0') {
			pam = {qr:"["+MyUtils.createQueryConditionBean("varietyId","=","?", selected,null) +"]",st:tipc,sco:'',dc:Dmlx.sort.desc};
		} 
		MyUtils._searchPanelData('dataPanel', 'impl_search/commonsPaginSearch.ajax', pam);
	});
};

$(function(){
	try {
		initPanel();
		loadTypeList();
	} catch (e) {
		showJsException(e,"数据加载失败!");
	}	
});

var submitAdd = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			MyUtils.openMask(_msg_process,win);
			$('#dt').val(dt);
			$('#dateEdit').attr("action",'impl_add/backFileUploadAdd.adm');
			$('#dateEdit').submit();
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var submitModify = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			$.messager.confirm(_msg_sys,MyUtils.valueAlign(_modify_confirm,"left"), function(r) {
				if(r) {
					MyUtils.openMask(_msg_process,win);
					$('#dt').val(dt);
					$('#dateEdit').attr("action",'impl_modify/backFileUploadModify.adm?qy=');
					$('#dateEdit').submit();
					
				}
			});
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var editValidator = function(){
	var editor_data = CKEDITOR.instances.content.getData();
	
	
	if (editor_data == null || editor_data == "") {
		editor_data = '';
	}
	var record = {
			 uuid:$('#uuid').val()
			,newsTitle:$('#newsTitle').val()
			,varietyId:$('#varietyId').val()
			,newsSrc:$('#newsSrc').val()
			,newsImg:$("#newsImg").val()
			,createTime:$("#createTime").datebox("getValue")
			,newsRec:$('input[name="newsRec"]:checked').val()
			,orderNum:$("#orderNum").val()
			,summary:$("#summary").val()
			,content:editor_data
			,lastNewsImg:$('#lastNewsImg').val()
		};
	
	//验证新闻名称
	if(MyUtils.isEmpty(record.newsTitle)) {
		MyUtils.waring("新闻名称不能为空");
		return null;
	}
	//验证排序是否数字
	if(MyUtils.isEmpty(record.orderNum)) {
		MyUtils.waring("排序格式不正确，请输入数字");
		return null;
	}
	return MyUtils.toJsonStr(record);
};



var getEditDiv = function(record) {
	var div= '<div id="editform"><iframe name="ifrName" src="" id="ifrName" style="display:none;"></iframe><form target="ifrName" action="" method="POST" name="fmName" id="dateEdit" enctype="multipart/form-data">'
		+'<table cellspacing="2" cellpadding="2">'
		+'<tbody>'
		+'<tr>'
		+'<th align="right" width=200>新闻标题：</th>'
		+'<td align="left" width=500>'
		+'<input type="hidden" id="uuid" value="" >'
		+'<input type="text" maxlength="120" id="newsTitle" style="width: 288px;" value=""><label class="requre_field">*</label>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>新闻分类：</th>'
		+'<td align="left" width=500>'
		+'<select id="varietyId" style="width: 208px;"><option value="-1">公告</option><option value="-2">热点新闻</option></select>'
		+'</td>'
		+'</tr>'
		+'<th align="right" width=200>新闻来源：</th>'
		+'<td align="left" width=500>'
		+'<input id="newsSrc" style="width: 288px;" value=""/>'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>新闻图片：</th>'
		+'<td align="left" width=500>'
		+'<input type="file" name="file" id="newsImg" style="width: 288px;" value="" size =40>'
		+'<div style="color:gray;">上传图片！【75px*60px最佳】</div>	'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>发布日期：</th>'
		+'<td align="left"> <input type="text"  id="createTime" style="width: 130px;" value=""></td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>是否推荐：</th>'
		+'<td align="left">'
		+'<input type = "radio" name="newsRec"  value="1"	>是</input>'
		+'<input type = "radio" name="newsRec"  value="0"	>否</input></td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" width=200>排序：</th>'
		+'<td align="left" width=500>'
		+'<input type="text"  maxlength="5" id="orderNum" style="width: 288px;" value="">'
		+'</td>'
		+'</tr>'
		+'<tr>'
		+'<th align="right" valign="top" width=200>文章摘要：</th>'
		+'<td align="left"> <textarea rows="5"  id="summary" style="width: 600px;" ></textarea></td>'
		+'</tr>'
		
		+'<tr>'
		+'<th align="right" width=200></th>'
		+'<td align="left">'
		+'<textarea id="content" name="content" rows="10" style="width: 600px;"></textarea>'	
		+'</td>'
		+'</tr>'
		+'</tbody>'
		+'</table>'
		+'<input type="hidden" id="lastNewsImg" value="" >'
		+'<input type="hidden" id="dt" name="dt" value="" >'
		+'<input type="hidden" id="st" name="st" value="'+tipc+'" >'
		+'</form><div>';
	return div;
};

var initContent = function(){
	 if(!CKEDITOR.instances.content){  //判定content是否存在   
		 initCK();
	 }else{
		 CKEDITOR.instances["content"].destroy(true);
		 initCK();
	 }
	
};

var initCK = function(){
	CKEDITOR.replace('content',{
		height:screen.height/2 + 'px'
	   ,basePath:"ckfinder/"
	   ,skin: "kama"
	}); 
}



